/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Update;

import functions.Dogy;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Database;

/**
 *
 * @author JArik
 */
public class DBUpdate {

    public static int DB_VERSION = 4;

    public static int doUpdate(int curVersion) throws SQLException {
        if (curVersion < Dogy.DB_VERSION) {
            throw new SQLException("Wrong version");
        }

        switch (curVersion) {
            case 0:      // init version

                curVersion = doUpdate(++curVersion);
                break;
            case 1:      // slight changes in tables
                update_v1_v2();
                curVersion = doUpdate(++curVersion);
                break;
            case 2:      // add dead dogs & cardPayments
                update_v2_v3();
                curVersion = doUpdate(++curVersion);
                break;
            case 3:     // add currency support
                update_v3_v4();
                curVersion = doUpdate(++curVersion);
                break;
            default:
                return curVersion;
        }
        return curVersion;
    }

    public static void update_v1_v2() throws SQLException {

        Database db = new Database();
        db.updateDBInfo("ALTER TABLE `visits` CHANGE `recom` `recom` MEDIUMTEXT NULL");
        db.updateDBInfo("ALTER TABLE `healing` CHANGE `name` `name` MEDIUMTEXT NULL");
        db.updateDBInfo("ALTER TABLE `healing` CHANGE `result` `result` MEDIUMTEXT NULL");
        //
        db.closeStatement();

    }

    public static void update_v2_v3() throws SQLException {
        //update_v1_v2();
        Database db = new Database();
        try {
            db.updateDBInfo("ALTER TABLE `dogs` ADD `deathDate` DATE NULL AFTER `age`");
        } catch (Exception e) {
        }
        try {
            db.updateDBInfo("ALTER TABLE `visits` ADD `cardPayment` TINYINT UNSIGNED DEFAULT '0' NULL AFTER `deleted`");
        } catch (Exception e) {
        }

        db.closeStatement();
    }

    public static void update_v3_v4() throws SQLException {
        //update_v2_v3();
        Database db = new Database();
        db.updateDBInfo("ALTER TABLE `sklad_drugs`  ADD COLUMN `currency` VARCHAR(10) NULL DEFAULT NULL AFTER `sellHigh`");
        db.updateDBInfo("ALTER TABLE `visits`  ADD COLUMN `currency` VARCHAR(10) NULL DEFAULT NULL AFTER `visitOtherPayed`");
        db.updateDBInfo("ALTER VIEW `skladdetailed` AS select `sd`.`rec_id` AS `rec_id`,`sd`.`id` AS `id`,`sd`.`buy` AS `buy`,`sd`.`sell` AS `sell`,`sd`.`sellMedium` AS `sellMedium`,`sd`.`sellHigh` AS `sellHigh`,`sd`.`tax` AS `tax`,`sd`.`currency` AS `currency`, `sd`.`incomeAmount` AS `incomeAmount`,`sd`.`currentAmount` AS `currentAmount`,`sd`.`descr` AS `descr`,`sd`.`date` AS `date`,`sd`.`distrId` AS `distrId`,`sd`.`skl` AS `skl`,`sn`.`name` AS `skladName`,`sn`.`descr` AS `skladDescr` from (`dogy`.`sklad_drugs` `sd` left join `dogy`.`sklad_names` `sn` on((`sd`.`skl` = `sn`.`id`)))");

        db.updateDBInfo("UPDATE `sklad_drugs` SET currency='EEK' WHERE currency IS NULL");
        db.updateDBInfo("UPDATE `visits` SET currency='EEK' WHERE currency IS NULL");
        
        db.closeStatement();
    }
}
